﻿using System;
using System.Collections.Generic;
using System.Text;
using ShootSearch.Common;
using ShootSearch.Core;
using ShootSearch.Util.Serializers;

namespace ShootSearch.Spiders
{
    /// <summary>
    /// SpiderProgress
    /// </summary>
    public class SpiderProgress : SpiderManConfig
    {
        public SpiderAction Action { get; set; }
        public List<Url> UpdateList { get; set; }

        private int fileSystemID = 0;
        /// <summary>
        /// 最后使用的文件系统标识符
        /// </summary>
        public int FileSystemID
        {
            get { return fileSystemID; }
            set { fileSystemID = value; }
        }

        public SpiderProgress()
        {
            UpdateList = new List<Url>();
        }



        public static SpiderProgress Load(string path)
        {
            SpiderProgress p = new SpiderProgress();
            p = XmlFormatter.FromFile(path, p.GetType()) as SpiderProgress;
            if (p == null) p = new SpiderProgress();
            return p;
        }

        public static Dictionary<string, short> GetUpdateUrlList(List<Url> urls)
        {
            Dictionary<string, short> list = new Dictionary<string, short>();
            if (urls != null)
            {
                for (int i = 0; i < urls.Count; i++)
                {
                    if (!list.ContainsKey(urls[i].Address))
                    {
                        list.Add(urls[i].Address, urls[i].Depth);
                    }
                }
            }
            return list;
        }

        public static List<Url> UpdateUrlList(Dictionary<string, short> urlList)
            {
                List<Url> list = new List<Url>();
                if (urlList != null)
                {
                    Dictionary<string, short>.Enumerator erator = urlList.GetEnumerator();
                    while (erator.MoveNext())
                    {
                        urlList.Add(erator.Current.Key, erator.Current.Value);
                    }
                }
                return list;
            }
    }


    /// <summary>
    /// SpiderAction
    /// </summary>
    public enum SpiderAction
    {
        NotStarted,
        Processing,
        Updating
    }
}
